<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Exception\Handler;

use App\Constants\ErrorCode;
use App\Exception\HeaderErrorException;
use App\Utils\Helper;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Throwable;

class HeaderErrorExceptionHandler extends ExceptionHandler
{
    public function __construct(protected StdoutLoggerInterface $logger)
    {
    }

    public function handle(Throwable $throwable, ResponseInterface $response)
    {
        $this->stopPropagation();

        return $response->withStatus(200)
            ->withHeader('Content-Type','application/json;Charset=utf8')
            ->withBody(new SwooleStream(Helper::jsonBody(ErrorCode::HEADER_FIELD_ERR, )));

    }

    public function isValid(Throwable $throwable): bool
    {
        return $throwable instanceof HeaderErrorException;
    }
}
